package com.tianma315.admin.qa.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.qa.domain.GlassBottleLineDO;
import com.tianma315.admin.qa.domain.PlasticBottleLineDO;
import com.tianma315.admin.qa.service.GlassBottleLineService;
import com.tianma315.admin.qa.service.PlasticBottleLineService;
import com.tianma315.admin.qa.vo.QaPatrolVO;
import com.tianma315.admin.sys.domain.UserDO;
import com.tianma315.admin.sys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.qa.dao.QaPatrolDao;
import com.tianma315.admin.qa.domain.QaPatrolDO;
import com.tianma315.admin.qa.service.QaPatrolService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 品控巡查
 * </pre>
 * <small> 2019-07-10 09:48:55 | wen</small>
 */
@Service
public class QaPatrolServiceImpl extends CoreServiceImpl<QaPatrolDao, QaPatrolDO> implements QaPatrolService {

    @Autowired
    private GlassBottleLineService glassBottleLineService;

    @Autowired
    private PlasticBottleLineService plasticBottleLineService;

    @Autowired
    private QaPatrolDao qaPatrolDao;


    @Autowired
    private UserService userService;

    @Override
    public Page<QaPatrolVO> getPatroVOPage(Integer pageNumber, Integer pageSize, QaPatrolVO qaPatrolDO,Date beginDate,
                                           Date endDate) {
        Page<QaPatrolVO> page = new Page<>(pageNumber,pageSize);
        List<QaPatrolVO> list = qaPatrolDao.getPatrolVOPage(page,qaPatrolDO,beginDate,endDate);
        page.setRecords(list);
        return page;
    }

    @Override
    public QaPatrolVO getQaPatrolById(Integer id) {
//        QaPatrolVO qaPatrolVO = new QaPatrolVO();
        QaPatrolVO qaPatrolVO =qaPatrolDao.selectListById(id);
//        if (qaPatrolDO.getAbnormalSituation() == 0){
//            GlassBottleLineDO glassBottleLineDO = glassBottleLineService.getGlassBottleLineByQaPatrolId(qaPatrolDO.getQaPatrolId());
//            if (glassBottleLineDO!=null){
//                BeanUtils.copyProperties(glassBottleLineDO,qaPatrolVO);
//            }
//        }else if(qaPatrolDO.getAbnormalSituation() == 1){
//            PlasticBottleLineDO plasticBottleLineDO = plasticBottleLineService.getPlasticBottleLineByQaPatrol(qaPatrolDO.getQaPatrolId());
//            if (plasticBottleLineDO!=null){
//                BeanUtils.copyProperties(plasticBottleLineDO,qaPatrolVO);
//            }
//
//        }
//        BeanUtils.copyProperties(qaPatrolDO,qaPatrolVO);
        return qaPatrolVO;
    }

    @Override
    @Transactional
    public Boolean addQaPatrol(QaPatrolVO qaPatrolVO) {

        QaPatrolDO qaPatrolDO = new QaPatrolDO();
        BeanUtils.copyProperties(qaPatrolVO,qaPatrolDO);
        Boolean result =insert(qaPatrolDO);
        //判断是玻璃瓶线还是塑料瓶线
//        Integer abnormalSituation = qaPatrolDO.getAbnormalSituation();
//        if (abnormalSituation.equals(0)){
//            //插入玻璃瓶线
//            GlassBottleLineDO glassBottleLineDO = new GlassBottleLineDO();
//            BeanUtils.copyProperties(qaPatrolVO,glassBottleLineDO);
//            glassBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//            glassBottleLineDO.setCreateBy(qaPatrolDO.getCreateBy());
//            glassBottleLineDO.setCreateDate(qaPatrolDO.getCreateDate());
//            glassBottleLineDO.setState(qaPatrolDO.getState());
//            glassBottleLineService.insert(glassBottleLineDO);
//        }else if(abnormalSituation.equals(1)){
//            PlasticBottleLineDO plasticBottleLineDO = new PlasticBottleLineDO();
//            BeanUtils.copyProperties(qaPatrolVO,plasticBottleLineDO);
//            plasticBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//            plasticBottleLineDO.setCreateBy(qaPatrolDO.getCreateBy());
//            plasticBottleLineDO.setCreateDate(qaPatrolDO.getCreateDate());
//            plasticBottleLineDO.setState(qaPatrolDO.getState());
//            plasticBottleLineService.insert(plasticBottleLineDO);
//        }
        return result;
    }

    @Override
    public Boolean deleteQaPatrol(Integer id) {
        QaPatrolDO qaPatrolDO = new QaPatrolDO();
        qaPatrolDO.setQaPatrolId(id);
        qaPatrolDO.setState(1);
        Boolean result = updateById(qaPatrolDO);
        return result;
    }

//    @Override
//    @Transactional
//    public Boolean updateQaPatrol(QaPatrolVO qaPatrolVO) {
//        QaPatrolDO qaPatrolDO = new QaPatrolDO();
//        BeanUtils.copyProperties(qaPatrolVO,qaPatrolDO);
//        Boolean result =  updateById(qaPatrolDO);
//        //AbnormalSituation为0是玻璃瓶线，1是塑料瓶线
//        if (qaPatrolDO.getAbnormalSituation().equals(0)){
//            GlassBottleLineDO glassBottleLineDO = glassBottleLineService.getGlassBottleLineByQaPatrolId(qaPatrolVO.getQaPatrolId());
//            //判断是否能获取到数据,如果为空则设置状态，创建人跟创建时间
//            if (glassBottleLineDO!=null){
//
//            }else{
//                glassBottleLineDO = new GlassBottleLineDO();
//                glassBottleLineDO.setState(0);
//                glassBottleLineDO.setCreateBy(qaPatrolVO.getCreateBy());
//                glassBottleLineDO.setCreateDate(new Date());
//                glassBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//            }
//            glassBottleLineDO.setMaterialCenterTemperature(qaPatrolVO.getMaterialCenterTemperature());
//            glassBottleLineDO.setSterilizationDate(qaPatrolVO.getSterilizationDate());
//            glassBottleLineDO.setWaterTemperature(qaPatrolVO.getWaterTemperature());
//            glassBottleLineService.insertOrUpdate(glassBottleLineDO);
//            //把塑料瓶线状态设为1，表示删除
//            PlasticBottleLineDO plasticBottleLineDO = plasticBottleLineService.getPlasticBottleLineByQaPatrol(qaPatrolVO.getQaPatrolId());
//            if (plasticBottleLineDO!=null){
//                plasticBottleLineDO.setState(1);
//                plasticBottleLineService.updateById(plasticBottleLineDO);
//            }
//
//        }else{
//            PlasticBottleLineDO plasticBottleLineDO = plasticBottleLineService.getPlasticBottleLineByQaPatrol(qaPatrolVO.getQaPatrolId());
//            //判断是否能获取到数据,如果为空则设置状态，创建人跟创建时间
//            if (plasticBottleLineDO!=null){
//
//            }else{
//                plasticBottleLineDO = new PlasticBottleLineDO();
//                plasticBottleLineDO.setState(0);
//                plasticBottleLineDO.setCreateBy(qaPatrolVO.getCreateBy());
//                plasticBottleLineDO.setCreateDate(new Date());
//                plasticBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//            }
//            plasticBottleLineDO.setDesaltingWaterTemperature(qaPatrolVO.getDesaltingWaterTemperature());
//            plasticBottleLineDO.setMaterialTemperature(qaPatrolVO.getMaterialTemperature());
//            plasticBottleLineService.insertOrUpdate(plasticBottleLineDO);
//
//            GlassBottleLineDO glassBottleLineDO = glassBottleLineService.getGlassBottleLineByQaPatrolId(qaPatrolVO.getQaPatrolId());
//            if (glassBottleLineDO!=null){
//                glassBottleLineDO.setState(1);
//                glassBottleLineService.updateById(glassBottleLineDO);
//            }
//        }
//        return result;
//    }
@Override
@Transactional
public Boolean updateQaPatrol(QaPatrolVO qaPatrolVO) {
    QaPatrolDO qaPatrolDO = new QaPatrolDO();
    BeanUtils.copyProperties(qaPatrolVO,qaPatrolDO);
    Boolean result =  updateById(qaPatrolDO);
    //AbnormalSituation为0是玻璃瓶线，1是塑料瓶线
//    if (qaPatrolDO.getAbnormalSituation().equals(0)){
//        GlassBottleLineDO glassBottleLineDO = glassBottleLineService.getGlassBottleLineByQaPatrolId(qaPatrolVO.getQaPatrolId());
//        //判断是否能获取到数据,如果为空则设置状态，创建人跟创建时间
//        if (glassBottleLineDO!=null){
//
//        }else{
//            glassBottleLineDO = new GlassBottleLineDO();
//            glassBottleLineDO.setState(0);
//            glassBottleLineDO.setCreateBy(qaPatrolVO.getCreateBy());
//            glassBottleLineDO.setCreateDate(new Date());
//            glassBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//        }
//        glassBottleLineDO.setMaterialCenterTemperature(qaPatrolVO.getMaterialCenterTemperature());
//        glassBottleLineDO.setSterilizationDate(qaPatrolVO.getSterilizationDate());
//        glassBottleLineDO.setWaterTemperature(qaPatrolVO.getWaterTemperature());
//        glassBottleLineService.insertOrUpdate(glassBottleLineDO);
//        //把塑料瓶线状态设为1，表示删除
//        PlasticBottleLineDO plasticBottleLineDO = plasticBottleLineService.getPlasticBottleLineByQaPatrol(qaPatrolVO.getQaPatrolId());
//        if (plasticBottleLineDO!=null){
//            plasticBottleLineDO.setState(1);
//            plasticBottleLineService.updateById(plasticBottleLineDO);
//        }
//
//    }else{
//        PlasticBottleLineDO plasticBottleLineDO = plasticBottleLineService.getPlasticBottleLineByQaPatrol(qaPatrolVO.getQaPatrolId());
//        //判断是否能获取到数据,如果为空则设置状态，创建人跟创建时间
//        if (plasticBottleLineDO!=null){
//
//        }else{
//            plasticBottleLineDO = new PlasticBottleLineDO();
//            plasticBottleLineDO.setState(0);
//            plasticBottleLineDO.setCreateBy(qaPatrolVO.getCreateBy());
//            plasticBottleLineDO.setCreateDate(new Date());
//            plasticBottleLineDO.setQaPatrolId(qaPatrolDO.getQaPatrolId());
//        }
//        plasticBottleLineDO.setDesaltingWaterTemperature(qaPatrolVO.getDesaltingWaterTemperature());
//        plasticBottleLineDO.setMaterialTemperature(qaPatrolVO.getMaterialTemperature());
//        plasticBottleLineService.insertOrUpdate(plasticBottleLineDO);
//
//        GlassBottleLineDO glassBottleLineDO = glassBottleLineService.getGlassBottleLineByQaPatrolId(qaPatrolVO.getQaPatrolId());
//        if (glassBottleLineDO!=null){
//            glassBottleLineDO.setState(1);
//            glassBottleLineService.updateById(glassBottleLineDO);
//        }
//    }
    return result;
}

    @Override
    public QaPatrolVO getQaPatroProductIdAndBatch(Long productId, String batch) {
        Wrapper<QaPatrolDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",productId);
        wrapper.eq("batch",batch);
        wrapper.eq("state",0);
        QaPatrolDO qaPatrolDO = selectOne(wrapper);
        QaPatrolVO qaPatrolVO = new QaPatrolVO();
        if (qaPatrolDO !=null){
            UserDO user = userService.selectById(qaPatrolDO.getCheckBy());
            BeanUtils.copyProperties(qaPatrolDO,qaPatrolVO);
            qaPatrolVO.setCheckName(user.getName());
        }
        return qaPatrolVO;
    }

    @Override
    public List<QaPatrolVO> getQaPatroByBatch(String batch) {
        List<QaPatrolVO> qaPatrolVO = qaPatrolDao.getQaPatroByBatch(batch);
        return qaPatrolVO;
    }
}
